package threedee;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import threedee.generator.*;

public class App extends PApplet {
	private CityPlan cp;
	private float rotateX = (float) 0.8;
	private float rotateZ = (float) -1.75;

	private float mouseXWas = -1;
	private float mouseYWas = -1;
	
	boolean fistTime = true;

	//0 Building 1 texture
	//1 Building 2 texture
	//2 Roof1 top texture
	//3 Roof2 top texture
	//4 Catwalk side texture
	//5 Catwalk top texture

	private PImage[] texture;

	public void setup(){
		this.size(800, 600);
		this.texture = new PImage[7];

		texture[0] = loadImage("threedee/images/walltexture1.png");
		texture[1] = loadImage("threedee/images/walltexture2.png");
		texture[2] = loadImage("threedee/images/roof1.png");
		texture[3] = loadImage("threedee/images/roof2.png");
		texture[4] = loadImage("threedee/images/border1.png");
		texture[5] = loadImage("threedee/images/roof1.png");
		texture[6] = loadImage("threedee/images/street.png");
		cp = new CityPlan(this.texture);
	}

	public void draw(){
		background(0);

		PGraphics g = createGraphics(width, height, P3D);
		g.beginDraw();
		g.smooth();
		g.lights();

		g.rotateX((float) rotateX);
		g.rotateY(0);
		g.rotateZ((float) rotateZ);

		g.translate(-150, -100, -600);
		cp.draw(g);
		if(fistTime){
			cp.print();
		}
		g.endDraw();
		image(g, 0, 0);
		fistTime = false;
	}

	public void keyPressed() {
		if(keyCode == DOWN){
			rotateX += 0.01;
			if(rotateX > 1.0){
				rotateX = (float) 1.0;
			}
		}
		if(keyCode == UP){
			rotateX -= 0.01;
			if(rotateX < 0){
				rotateX = 0;
			}
		}
		if(keyCode == LEFT){
			rotateZ -= 0.1;
		}
		if(keyCode == RIGHT){
			rotateZ += 0.1;
		}	
	}

	public void mouseMoved(){
		if(pmouseX > mouseXWas){
			rotateZ += 0.01;
		}
		if(pmouseX < mouseXWas){
			rotateZ -= 0.01;
		}
		mouseXWas = pmouseX;
		if(pmouseY > mouseYWas){
			rotateX += 0.01;
			if(rotateX > 1.0){
				rotateX = (float) 1.0;
			}
		}
		if(pmouseY < mouseYWas){
			rotateX -= 0.01;
			if(rotateX < 0){
				rotateX = 0;
			}
		}
		mouseYWas = pmouseY;
	}
}
